<%@ Page Title="" Debug="true" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master"
    Inherits="System.Web.Mvc.ViewPage" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<%@ Import Namespace="System.Linq" %>
<%@ Import Namespace="CDIO1.Models" %>
<%@ Import Namespace="CDIO1.Controllers" %>

<script runat="server">

    protected void lbInsertTeachMethod_Click(object sender, EventArgs e)
    {
        string methodID = ((TextBox)gvTeachMethod.FooterRow.FindControl("txtMethodID")).Text;
        string methodName = ((TextBox)gvTeachMethod.FooterRow.FindControl("txtMethodName")).Text;
        if (methodID.CompareTo("") == 0 || methodName.CompareTo("") == 0)
        {
            return;
        }
        
        var db = new MethodDataContext();
        TeachingMethod teachMethod = new TeachingMethod();
        teachMethod.MethodID = methodID;
        teachMethod.Status = (int)Status.AVAILABLE;
        db.TeachingMethods.InsertOnSubmit(teachMethod);
        db.SubmitChanges();

        int ID = teachMethod.ID;
        TeachingMethodLanguage methodLanguage = new TeachingMethodLanguage();
        methodLanguage.TeachingMethodID = ID;
        methodLanguage.Method = methodName;
        methodLanguage.LanguageID = 1;
        methodLanguage.Status = 1;
        db.TeachingMethodLanguages.InsertOnSubmit(methodLanguage);
        db.SubmitChanges();
        
        Response.Redirect("~/Admin/ManageMethod");
    }

    protected void lbInsertEvalMethod_Click(object sender, EventArgs e)
    {
        string methodID = ((TextBox)gvEvalMethod.FooterRow.FindControl("txtMethodID")).Text;
        string methodName = ((TextBox)gvEvalMethod.FooterRow.FindControl("txtMethodName")).Text;
        if (methodID.CompareTo("") == 0 || methodName.CompareTo("") == 0)
        {
            return;
        }
        
        var db = new MethodDataContext();
        EvaluateMethod evalMethod = new EvaluateMethod();
        evalMethod.MethodID = methodID;
        evalMethod.Status = (int)Status.AVAILABLE;
        db.EvaluateMethods.InsertOnSubmit(evalMethod);
        db.SubmitChanges();

        int ID = evalMethod.ID;
        EvaluateMethodLanguage methodLanguage = new EvaluateMethodLanguage();
        methodLanguage.EvaluateID = ID;
        methodLanguage.Method = methodName;
        methodLanguage.LanguageID = 1;
        methodLanguage.Status = 1;
        db.EvaluateMethodLanguages.InsertOnSubmit(methodLanguage);
        db.SubmitChanges();
        
        Response.Redirect("~/Admin/ManageMethod");
    }

    protected void gvTeachMethod_DataBound(object sender, EventArgs e)
    {
        if (gvTeachMethod.Rows.Count == 0)
        {
            var db = new MethodDataContext();
            TeachingMethod teachMethod = new TeachingMethod();
            teachMethod.Status = (int)Status.AVAILABLE;
            db.TeachingMethods.InsertOnSubmit(teachMethod);
            db.SubmitChanges();
            Response.Redirect("~/Admin/ManageMethod");
        }
        
    }

    protected void gvEvalMethod_DataBound(object sender, EventArgs e)
    {
        if (gvEvalMethod.Rows.Count == 0)
        {
            var db = new MethodDataContext();
            EvaluateMethod evalMethod = new EvaluateMethod();
            evalMethod.Status = (int)Status.AVAILABLE;
            db.EvaluateMethods.InsertOnSubmit(evalMethod);
            db.SubmitChanges();
            Response.Redirect("~/Admin/ManageMethod");
        }
        
    }

    protected void gvTeachMethod_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = Convert.ToInt32(((Label)gvTeachMethod.Rows[e.RowIndex].FindControl("lbID")).Text);
        int methodLanguageID = Convert.ToInt32(((Label)gvTeachMethod.Rows[e.RowIndex].FindControl("lbMethodLanguageID")).Text);

        string methodID = ((TextBox)gvTeachMethod.Rows[e.RowIndex].FindControl("txtMethodID")).Text;
        methodID = methodID.Trim();
        string methodName = ((TextBox)gvTeachMethod.Rows[e.RowIndex].FindControl("txtMethodName")).Text;
        methodName = methodName.Trim();
        var db = new MethodDataContext();
        
        TeachingMethod t = db.TeachingMethods.Single(p => p.ID == ID);
        t.MethodID = methodID;
        db.SubmitChanges();

        TeachingMethodLanguage l = db.TeachingMethodLanguages.Single(p => p.ID == methodLanguageID);
        l.Method = methodName;
        db.SubmitChanges();

        gvTeachMethod.EditIndex = -1;
        Response.Redirect("/Admin/ManageMethod");      
    }

    protected void gvTeachMethod_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int ID = Convert.ToInt32(((Label)gvTeachMethod.Rows[e.RowIndex].FindControl("lbID")).Text);
        var db = new MethodDataContext();

        var l = from p in db.TeachingMethodLanguages
                where p.TeachingMethodID == ID
                select p;
        db.TeachingMethodLanguages.DeleteAllOnSubmit(l);
        db.SubmitChanges();

        TeachingMethod t = db.TeachingMethods.Single(p => p.ID == ID);
        db.TeachingMethods.DeleteOnSubmit(t);
        db.SubmitChanges();

        Response.Redirect("/Admin/ManageMethod");
    }

    protected void gvEvalMethod_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = Convert.ToInt32(((Label)gvEvalMethod.Rows[e.RowIndex].FindControl("lbID")).Text);
        int methodLanguageID = Convert.ToInt32(((Label)gvEvalMethod.Rows[e.RowIndex].FindControl("lbMethodLanguageID")).Text);

        string methodID = ((TextBox)gvEvalMethod.Rows[e.RowIndex].FindControl("txtMethodID")).Text;
        methodID = methodID.Trim();
        string methodName = ((TextBox)gvEvalMethod.Rows[e.RowIndex].FindControl("txtMethodName")).Text;
        methodName = methodName.Trim();
        var db = new MethodDataContext();

        EvaluateMethod t = db.EvaluateMethods.Single(p => p.ID == ID);
        t.MethodID = methodID;
        db.SubmitChanges();

        EvaluateMethodLanguage l = db.EvaluateMethodLanguages.Single(p => p.ID == methodLanguageID);
        l.Method = methodName;
        db.SubmitChanges();

        gvEvalMethod.EditIndex = -1;
        Response.Redirect("/Admin/ManageMethod");      
    }

    protected void gvEvalMethod_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int ID = Convert.ToInt32(((Label)gvEvalMethod.Rows[e.RowIndex].FindControl("lbID")).Text);
        var db = new MethodDataContext();

        var l = from p in db.EvaluateMethodLanguages
                where p.EvaluateID == ID
                select p;
        db.EvaluateMethodLanguages.DeleteAllOnSubmit(l);
        db.SubmitChanges();

        EvaluateMethod t = db.EvaluateMethods.Single(p => p.ID == ID);
        db.EvaluateMethods.DeleteOnSubmit(t);
        db.SubmitChanges();

        Response.Redirect("/Admin/ManageMethod");
    }
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    ManageMethod
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <asp:Label ID="lbLanguge" runat="server" Visible="False"></asp:Label>
    <h1>
        Teaching Method</h1>
         <asp:GridView ID="gvTeachMethod" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource1" ForeColor="#333333"
                GridLines="None" ShowFooter="True" OnDataBound="gvTeachMethod_DataBound" 
                        onrowupdating="gvTeachMethod_RowUpdating" 
                        onrowdeleting="gvTeachMethod_RowDeleting">
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID" 
                        Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="lbID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MethodLanguageID" InsertVisible="False" 
                        SortExpression="MethodLanguageID" Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="lbMethodLanguageID" runat="server" 
                                Text='<%# Eval("MethodLanguageID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbMethodLanguageID" runat="server" 
                                Text='<%# Bind("MethodLanguageID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MethodID" SortExpression="MethodID">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMethodID" runat="server" Text='<%# Bind("MethodID") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtMethodID" runat="server" Text='<%# Bind("MethodID") %>'></asp:TextBox>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("MethodID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MethodName" SortExpression="Method">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMethodName" runat="server" Text='<%# Bind("Method") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtMethodName" runat="server" Text='<%# Bind("MethodID") %>'></asp:TextBox>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Method") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:LinkButton ID="lbInsertTeachMethod" runat="server" OnClick="lbInsertTeachMethod_Click">Insert</asp:LinkButton>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="Edit"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                                Text="Delete"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>"
                
                
                        
                        SelectCommand="SELECT TeachingMethod.ID, TeachingMethod.MethodID, TeachingMethod.Status, TeachingMethodLanguage.Method, TeachingMethodLanguage.ID AS MethodLanguageID FROM TeachingMethod INNER JOIN TeachingMethodLanguage ON TeachingMethod.ID = TeachingMethodLanguage.TeachingMethodID WHERE (TeachingMethod.Status = 1) AND (TeachingMethodLanguage.LanguageID = @LanguageID)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="lbLanguge" DefaultValue="1" Name="LanguageID" 
                        PropertyName="Text" />
                </SelectParameters>
            </asp:SqlDataSource>
 
    
    <h1>Evaluation Method</h1>
     <asp:GridView ID="gvEvalMethod" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                CellPadding="4" DataKeyNames="ID" DataSourceID="SqlDataSource2" ForeColor="#333333"
                GridLines="None" ShowFooter="True" OnDataBound="gvEvalMethod_DataBound" 
                onrowupdating="gvEvalMethod_RowUpdating" 
                onrowdeleting="gvEvalMethod_RowDeleting">
                <EmptyDataRowStyle BackColor="LightBlue" ForeColor="Red" />
                <EmptyDataTemplate>
                    No Data Found.
                </EmptyDataTemplate>
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <Columns>
                    <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID" 
                        Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="lbID" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MethodLanguageID" InsertVisible="False" 
                        SortExpression="MethodLanguageID" Visible="False">
                        <EditItemTemplate>
                            <asp:Label ID="lbMethodLanguageID" runat="server" 
                                Text='<%# Eval("MethodLanguageID") %>'></asp:Label>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lbMethodLanguageID" runat="server" 
                                Text='<%# Bind("MethodLanguageID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MethodID" SortExpression="MethodID">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMethodID" runat="server" Text='<%# Bind("MethodID") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtMethodID" runat="server" Text='<%# Bind("MethodID") %>'></asp:TextBox>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("MethodID") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="MethodName" SortExpression="Method">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtMethodName" runat="server" Text='<%# Bind("Method") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:TextBox ID="txtMethodName" runat="server" Text='<%# Bind("MethodID") %>'></asp:TextBox>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label3" runat="server" Text='<%# Bind("Method") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False">
                        <EditItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel"></asp:LinkButton>
                        </EditItemTemplate>
                        <FooterTemplate>
                            <asp:LinkButton ID="lbInsertEvalMethod" runat="server" OnClick="lbInsertEvalMethod_Click">Insert</asp:LinkButton>
                        </FooterTemplate>
                        <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="Edit"></asp:LinkButton>
                            &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                                Text="Delete"></asp:LinkButton>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#999999" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CDIO1ConnectionString %>"
                
                
                SelectCommand="SELECT EvaluateMethod.ID, EvaluateMethod.MethodID, EvaluateMethodLanguage.ID AS MethodLanguageID, EvaluateMethodLanguage.Method FROM EvaluateMethod INNER JOIN EvaluateMethodLanguage ON EvaluateMethod.ID = EvaluateMethodLanguage.EvaluateID WHERE (EvaluateMethod.Status = 1) AND (EvaluateMethodLanguage.LanguageID = @LanguageID)">
                <SelectParameters>
                    <asp:ControlParameter ControlID="lbLanguge" DefaultValue="1" Name="LanguageID" 
                        PropertyName="Text" />
                </SelectParameters>
            </asp:SqlDataSource>
</asp:Content>
